package com.yb.web.controller.model;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yb.framework.core.domain.AjaxObject;
import com.yb.framework.core.utils.StringUtil;
import com.yb.framework.log.annotation.Log;
import com.yb.framework.log.enums.OperateTypeEnum;
import com.yb.module.model.domain.ModelTemplate;
import com.yb.web.controller.base.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/model/template")
@Api(tags = "模板管理")
public class ModelTemplateController extends BaseController {
	
	Logger logger = LoggerFactory.getLogger(ModelTemplateController.class.getName());
	
	private static String MODEL_TEMPLATE = "model/template/template";
	private static String MODEL_TEMPLATE_ADD = "model/template/template_add";
	private static String MODEL_TEMPLATE_EDIT = "model/template/template_edit";
	private static String MODEL_NEO4J_CHART = "model/neo4j/chart";
	
	@GetMapping()
	@PreAuthorize("hasAuthority('model:template')")
	public String main() {
		return MODEL_TEMPLATE;
	}
	
	@GetMapping("/add_view")
	@PreAuthorize("hasAuthority('model:template:add')")
	public String addView() {
		return MODEL_TEMPLATE_ADD;
	}
	
	@GetMapping("/edit_view/{id}")
	@PreAuthorize("hasAuthority('model:template:edit')")
	public String editView(@PathVariable("id") Long id, ModelMap model) {
		model.put("modelTemplate", modelTemplateService.getById(id));
		model.put("shapeIds", StringUtil.list2String(modelTemplateShapeService.getShapeIdsByTemplateId(id)));
		return MODEL_TEMPLATE_EDIT;
	}
	
	@GetMapping("/chart/{id}")
	@PreAuthorize("hasAuthority('model:template:chart')")
	public String chart(@PathVariable("id") Long id, ModelMap model) {
		model.put("label", "template");
		model.put("nodeUuid", id);
		return MODEL_NEO4J_CHART;
	}
	
	/**
	 * 模板列表
	 */
	@PostMapping("/list/{id}")
	@PreAuthorize("hasAuthority('model:template:list')")
	@ApiOperation(value="分页查询", notes="分页查询模板信息")
	public @ResponseBody List<ModelTemplate> list(@PathVariable("id") Long pid, ModelTemplate modelTemplate) {
		List<ModelTemplate> modelTemplateList = new ArrayList<ModelTemplate>();
		if(pid == 0) {
			List<ModelTemplate> topTemplates = modelTemplateService.getListByEntity(new ModelTemplate(0L));
			for (ModelTemplate topTemplate : topTemplates) {
				topTemplate.setText(topTemplate.getName());
				topTemplate.setState("closed");
				modelTemplateList.add(topTemplate);
			}
		} else {
			List<ModelTemplate> childTemplates = modelTemplateService.getListByEntity(new ModelTemplate(pid));
			for (ModelTemplate childTemplate : childTemplates) {
				childTemplate.setText(childTemplate.getName());
				childTemplate.setState("closed");
				modelTemplateList.add(childTemplate);
			}
		}
		return modelTemplateList;
	}
	
	/**
	 * 新增模板
	 */
	@PostMapping("/add")
	@PreAuthorize("hasAuthority('model:template:add')")
	@ApiOperation(value="新增模板", notes="新增模板信息")
	@Log(label = "新增模板", operateType = OperateTypeEnum.ADD)
	public @ResponseBody AjaxObject add(ModelTemplate modelTemplate) {
		return toAjax(modelTemplateService.insertTemplate(modelTemplate), "新增模板" + "[" + modelTemplate.getName() + "]");
	}
	
	/**
	 * 编辑模板
	 */
	@PostMapping("/edit")
	@PreAuthorize("hasAuthority('model:template:edit')")
	@ApiOperation(value="编辑模板", notes="编辑模板信息")
	@Log(label = "编辑模板", operateType = OperateTypeEnum.EDIT)
	public @ResponseBody AjaxObject edit(ModelTemplate modelTemplate) {
		return toAjax(modelTemplateService.updateTemplate(modelTemplate), "编辑模板" + "[" + modelTemplate.getName() + "]");
	}
	
	/**
	 * 删除模板
	 */
	@PostMapping("/delete")
	@PreAuthorize("hasAuthority('model:template:delete')")
	@ApiOperation(value="删除模板", notes="批量删除模板信息")
	@Log(label = "删除模板", operateType = OperateTypeEnum.DELETE)
	public @ResponseBody AjaxObject delete(@RequestParam("ids") String ids) {
		List<String> modelTemplateNames = modelTemplateService.getNamesByIds(StringUtil.str2LongArray(ids));
		return toAjax(modelTemplateService.deleteTemplate(StringUtil.str2LongArray(ids)), "批量删除模板" + modelTemplateNames);
	}
	
	/**
	 * 树形下拉框
	 * 获取节点的所有父ID
	 * 格式："id1, id2, id3"
	 */
	@RequestMapping("/getFatherIds/{id}")
	public @ResponseBody String getFatherIds(@PathVariable Long id) {
		return StringUtil.list2String(modelTemplateService.getAllParentIds(id, new ArrayList<Long>()));
	}
}